﻿using DbEntityTools.Model;
using HelpTools.Helpers;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;

namespace DbEntityTools.Service
{
    public class DbAccountService
    {
        private static string encryptKey = "EntityDb";
        private static string dirPath = Path.Combine(Application.StartupPath, "config");
        private static string filePath = Path.Combine(dirPath, "db");
        public static List<DbConfigModel> DbConfigs = new List<DbConfigModel>();

        static DbAccountService()
        {
            var accountStr = FileHelper.ReadFile(filePath);
            if (string.IsNullOrWhiteSpace(accountStr) == false)
            {
                var s = EncryptionHelper.DecryptDES(accountStr, encryptKey);
                DbConfigs = JsonConvert.DeserializeObject<List<DbConfigModel>>(s);
            }
        }

        public static void SaveDbAccount()
        {
            var accountStr = JsonConvert.SerializeObject(DbConfigs);
            var content = EncryptionHelper.EncryptDES(accountStr, encryptKey);
            FileHelper.SaveFile(dirPath, filePath, content);
        }
    }
}
